﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("ChineseFoodConn").ConnectionString)
            Dim sql = New String("SELECT * FROM Profiles WHERE TravellerName=@f1")
            Dim cmd = New OleDbCommand(sql, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows() Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_FristName.Text = dr("FirstName") 'there may have a problem if dont have IsDBNull
                If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Gender")) Then ddl_Gender.SelectedValue = dr("Gender")
                If Not IsDBNull(dr("EmailAddress")) Then tb_EmailAddress.Text = dr("EmailAddress")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
            End If
        End If
    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click
        
        If (fu_FileUpload.HasFile) = True Then
            fu_FileUpload.SaveAs(Request.MapPath("ProfilePhotos") & "/" & fu_FileUpload.FileName)
        End If

        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("ChineseFoodConn").ConnectionString)
        Dim sql = New String("UPDATE Profiles SET FirstName=@f1, LastName=@f2, Gender=@f3, EmailAddress=@f4, Description=@f5, Picture=@f6 WHERE TravellerName=@f7")
        Dim cmd = New OleDbCommand(sql, conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FristName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", ddl_Gender.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", tb_EmailAddress.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Description.Text)


        If (fu_FileUpload.HasFile) = True Then
            cmd.Parameters.AddWithValue("@f6", "ProfilePhotos/" & fu_FileUpload.FileName)  'have photo, then upload the photo, and use this photo
        ElseIf (ddl_Gender.SelectedValue = "Male") Then
            cmd.Parameters.AddWithValue("@f6", "ProfilePhotos/Default_Photo_QQBoy.png")    'dont have photo, and if gender is Male, then use default boy photo
        Else
            cmd.Parameters.AddWithValue("@f6", "ProfilePhotos/Default_Photo_QQGirl.jpg")   'dont have photo, and if gender is Female, then use default girl photo
        End If

        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
        conn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)
    End Sub
End Class